// pages/gc/commodity/commodity.js
const app =  getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      commodity:'',
      title:'',
      img:[],
      img_index:0
  },

  // 点击切换图片展示
  img_show(e){
        this.setData({
            img_index:e.currentTarget.dataset.index
        })
  },
  // 确定函数
  gm:function(){
    console.log(this.data.commodity)
    wx.showModal({
      title:this.data.title,
      content:'确定'+this.data.title+'后将与卖家开启交谈',
      success:(res)=>{
            if(res.confirm){
              wx.cloud.database().collection('yw_commodity').doc(this.data.commodity._id).update({
                data:{
                        status:'underway',
                        userId2:app.globalData.userInfo._id,
                        status_l:'进行中',
                        methods:this.data.title
                      }}).then((res)=>{
                  wx.showToast({
                    title: '易物确认',
                  })
                  let a =[]
                  wx.getStorage({
                    key: 'Messages',
                    success:res=>{
                      console.log(res)
                      a= res.data
                    },
                    fail:res=>{
                      console.log(res)
                    },
                    complete:res=>{
                      a.push(this.data.commodity.userId)
                      wx.setStorage({
                        data: a,
                        key: 'Messages',
                      })
                    }
                  })
                  let b =this.data.commodity
                  wx.cloud.database().collection('yw_exchange').where({openid:app.globalData.userInfo._openid,openid2:b.userId}).get().then(res=>{
                      if(res.data.length==0){
                        wx.cloud.database().collection('yw_exchange').add({
                          data:{
                            msg:[],
                            openid:app.globalData.userInfo._openid,
                            openid2:b.userId,
                            name:b.chatName,
                            sex:app.globalData.userInfo.gender=='男'?0:1
                          }
                        }).then(()=>{
                          wx.cloud.database().collection('yw_exchange').add({
                            data:{
                              msg:[],
                              openid2:app.globalData.userInfo._openid,
                              openid:b.userId,
                              name:app.globalData.userInfo.chatName,
                              sex:b.sex=='男'?0:1
                            }
                          }).then(()=>{
                            setTimeout(function(){
                              wx.navigateTo({
                               url: '../../lt/chata/chata?userId='+b.userId+"&&name="+b.chatName+"&&sex="+b.sex+"&&tishi="+b.name
                             })
                               },500)
                          })
                        })
                      }else{
                        setTimeout(function(){
                          wx.navigateTo({
                           url: '../../lt/chata/chata?userId='+b.userId+"&&name="+b.chatName+"&&sex="+b.sex+"&&tishi="+b.name
                         })
                           },500)
                      }
                  })

             })
            }else{
              wx.showToast({
                title: '易物取人',
              })
            }
      }
  })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that =this  
      wx.cloud.database().collection('yw_commodity').doc(options.id).get().then(res=>{
        that.setData({
          commodity:res.data,
          title:options.title
        })
        let img = this.data.commodity.picture
        img.forEach((item,index)=>{
          wx.cloud.getTempFileURL({
            fileList:[
              item
            ]
          }).then(res=>{
            let ls = this.data.img
            ls.push(res.fileList[0].tempFileURL)
              this.setData({
                img:ls
              })
              console.log(this.data.commodity)
          })
        })
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})